//
//  NewsDetailViewController.m
//  Pink
//
//  Created by 杨成阳 on 15/12/14.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "MainViewController.h"
#import "ChatViewController.h"
@interface NewsDetailViewController ()

@property (strong, nonatomic) UIImageView *headImageView;
@property (strong, nonatomic) UILabel *nickName;
@property (strong, nonatomic) UILabel *userPushCount;
@property (strong, nonatomic) YYLabel *NewsTitle;
@property (nonatomic,strong)News *newsObject;
@property (nonatomic,strong)UIScrollView *scrollView;
@property (strong, nonatomic) UILabel *NewsDate;
@property (nonatomic,strong) ChatViewController *chatView;

@end

@implementation NewsDetailViewController



- (instancetype)initWithData:(News *)news
{
    self = [super init];
    if (self) {
        _newsObject = news;
        [self initSubviews];
        [self initDatas];
        [self initBottomViews];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"新闻详情";
    self.navigationItem.hidesBackButton = YES;
    // Do any additional setup after loading the view.
    
    
}


- (void)initSubviews
{
     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 114)];
    [self.view addSubview:_scrollView];
    self.headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultHead"]];
    [_scrollView addSubview:self.headImageView];
    
    _headImageView.frame = CGRectMake(10,10,60,60);
    _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        
        NSLog(@"2");
        
    }];
    [_headImageView addGestureRecognizer:tap];
    
    self.nickName = [[UILabel alloc] initWithFrame:CGRectMake(73, 10, self.view.size.width - 80 , 20)];
    self.nickName.font = [UIFont boldSystemFontOfSize:17];
    self.nickName.textColor = [UIColor blackColor];
    [_scrollView addSubview:self.nickName];
    
    UIImageView *pushStar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pushCount"]];
    pushStar.frame = CGRectMake(73, 30, 18, 15);
    [_scrollView addSubview:pushStar];
    self.userPushCount = [[UILabel alloc] initWithFrame:CGRectMake(95, 30, 100, 15)];
    _userPushCount.font = [UIFont systemFontOfSize:15];
    _userPushCount.textColor = [UIColor lightGrayColor];
    [_scrollView addSubview:_userPushCount];
    
    _NewsDate = [[UILabel alloc] initWithFrame:CGRectMake(73, CGRectGetMaxY(_userPushCount.frame) + 2, 150, 20)];
    _NewsDate.textColor = [UIColor lightGrayColor];
    _NewsDate.font = [UIFont systemFontOfSize:12];
    [_scrollView addSubview:_NewsDate];
    
    
    _NewsTitle = [YYLabel new];
    _NewsTitle.frame = CGRectMake(10, CGRectGetMaxY(_headImageView.frame) + 15, 0, 0);
    if (_newsObject.title) {
        
        NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:_newsObject.title];
        titleText.font = [UIFont boldSystemFontOfSize:18];
        titleText.color = [UIColor blackColor];
        YYTextContainer *container = [YYTextContainer new];
        container.size = CGSizeMake(self.view.size.width - 20, CGFLOAT_MAX);
        container.maximumNumberOfRows = 3;
        
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:titleText];
        
        // 创建属性字符串
        
        _NewsTitle.size = layout.textBoundingSize;
        _NewsTitle.textLayout = layout;
        
    }
    
    [_scrollView addSubview:_NewsTitle];
    
    UIButton *originalUrl = [UIButton buttonWithType:UIButtonTypeCustom];
    [originalUrl setTitle:@"原文链接" forState:UIControlStateNormal];
    [originalUrl setTitleColor:[UIColor skyBlueColor] forState:UIControlStateNormal];
    if (_newsObject.originUrl) {
        originalUrl.frame = CGRectMake(10, CGRectGetMaxY(_NewsTitle.frame)+ 5, 60, 15);
    }else{
        originalUrl.frame = CGRectMake(10, CGRectGetMaxY(_NewsTitle.frame)+ 5, 60, 0);
    }
    originalUrl.titleLabel.font = [UIFont systemFontOfSize:15];
    [originalUrl addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
       
        NSLog(@"%@",_newsObject.originUrl);
        
    }];
    [_scrollView addSubview:originalUrl];
    
    NSArray *content = [_newsObject.content objectFromJSONString];
    YYLabel *contentLabel = [YYLabel new];
    contentLabel.displaysAsynchronously = YES; //开启异步绘制
    contentLabel.ignoreCommonProperties = YES; //忽略除了 textLayout 之外的其他属性
    
    // 创建文本容器
    YYTextContainer *contentcontainer = [YYTextContainer new];
    contentcontainer.size = CGSizeMake(self.view.bounds.size.width - 20, CGFLOAT_MAX);
    contentcontainer.maximumNumberOfRows = 0;
    
    NSMutableAttributedString *attributeContent = [Commen  parseTheContentWithContent:content font:[UIFont systemFontOfSize:16] color:[UIColor grayColor]];
    contentLabel.attributedText = attributeContent;
    // 生成排版结果
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:contentcontainer text:attributeContent];
    
    // 创建属性字符串
    
    contentLabel.size = layout.textBoundingSize;
    contentLabel.textLayout = layout;
    contentLabel.frame = CGRectMake(10, CGRectGetMaxY(originalUrl.frame) + 5, contentLabel.size.width, contentLabel.size.height);
    
    [_scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, CGRectGetMaxY(contentLabel.frame))];
    [_scrollView addSubview:contentLabel];
    
    _chatView = [[ChatViewController alloc] initWithConversationId:_newsObject.conversationId];
    _chatView.view.frame = CGRectMake(0, 0, self.view.size.width, CGRectGetMaxY(_scrollView.frame));
    [self addChildViewController:_chatView];
    [self.view addSubview:_chatView.view];
    _chatView.view.alpha = 0;
    
}

- (void)initBottomViews
{
    
    NSArray *buttonImages = @[@"backButton",@"notPush",@"chatRoom",@"letPush",@"collectIt"];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_scrollView.frame), self.view.size.width, 50)];
    bottomView.backgroundColor = COLORSTRING(@"2C3E50")
    [self.view addSubview:bottomView];
    for (int i = 0 ; i < buttonImages.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:buttonImages[i]] forState:UIControlStateNormal];
        button.center = CGPointMake(self.view.size.width / 10 + (self.view.size.width / 5) * i, 25);
        button.bounds = CGRectMake(0, 0, 20, 20);
        button.tag = 200 + i;
        [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:button];
    }
    
    
    
}

- (void)buttonTaped:(UIButton *)sender
{
    
    switch (sender.tag) {
        case 200:
            [self.navigationController popViewControllerAnimated:YES];
            break;
        case 201:
            
            
            
            break;
        case 202:{
            
            
            
            
        }
            break;
        case 203:
            
            break;
        case 204:
            
            break;
            
        default:
            break;
    }
    
}

-(void)initDatas
{
    WS(weakself)
    
    AVQuery *userQuery = [AVUser query];
    userQuery.cachePolicy = kAVCachePolicyNetworkElseCache;
    userQuery.maxCacheAge = 24 * 3600 * 3;
    [userQuery getObjectInBackgroundWithId:_newsObject.userId ?_newsObject.userId : @""  block:^(AVObject *object, NSError *error) {
        if (object) {
            [weakself.headImageView setImageWithURL:[NSURL URLWithString:((AVFile *)object[@"avatar"]).url] options:YYWebImageOptionProgressiveBlur |YYWebImageOptionSetImageWithFadeAnimation];
            weakself.nickName.text = object[@"nickName"]?object[@"nickName"] : ((AVUser *)object).username;
            weakself.userPushCount.text = object[@"pushCount"]?[object[@"pushCount"] description]:@"0";
            
        }else{
            weakself.nickName.text = @"";
            weakself.userPushCount.text = @"0";
        }
    }];
    
    
    self.NewsDate.text = [Commen simpleDateWithDate:_newsObject.createdAt];
    
    
    
}

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPop) {
        PingInvertTransition *pingInvert = [[PingInvertTransition alloc] initWithTransitionThroughButton:((MainViewController *)toVC).fromButton];
        return pingInvert;
    }else{
        return nil;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
